using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    public class Condition : Component 
    {
        public NodeVector OutputNodes;

        System.Collections.ArrayList alAnds;
        System.Collections.ArrayList alOrs;

        public Condition(Component poParentComponent, string psName)
            : base(poParentComponent, psName)
        {
            OutputNodes = this.CreateNodeVector("CONDITION_O",1);
            alAnds = new System.Collections.ArrayList();
            alOrs = new System.Collections.ArrayList();
        }

        public void Or(NodeVector pnvIn)
        {
            if(!alOrs.Contains(pnvIn))
                alOrs.Add(pnvIn);
        }

        public void And(NodeVector pnvIn)
        {
            if(!alAnds.Contains(pnvIn))
                alAnds.Add(pnvIn);
        }

        public override void GenerateStructure()
        {
            if (alAnds.Count > 0)
            {
                if (alAnds.Count == 1)
                {
                    Or((NodeVector)alAnds[0]);
                }
                else
                {
                    Or(Operators.AND(this,(NodeVector[])alAnds.ToArray(typeof(NodeVector))));
                }
            }

            if (alOrs.Count == 0)
            {
                //System.Diagnostics.Debug.Fail("Empty Conditional named " + HirarchicalName);
                OutputNodes.Join(0);
                return;
            }

            if (alOrs.Count == 1)
            {
                NodeVector oOut = (NodeVector)alOrs[0];
                OutputNodes.Connection = oOut; 
            }
            else
            {
                OutputNodes.Connection = Operators.OR(this,(NodeVector[])alOrs.ToArray(typeof(NodeVector)));
            }
        }

    }
}
